<%-- 
    Document   : homePage
    Created on : 2008-2-24, 10:12:56
    Author     : 
--%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ taglib prefix="pa" uri="http://agil.com"%>
<%@ page isELIgnored="false"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<%@page import="com.agil.photoalbum.db.dao.Functions"%>
<%@page import="com.agil.photoalbum.db.entities.Category"%>
<%@page import="java.util.List"%>
<%@page import="com.agil.photoalbum.db.entities.Picture"%>
<%@page import="com.agil.photoalbum.db.entities.User"%>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Страницата на Пешо</title>
        <link type="text/css" rel="stylesheet" href="/photo-album/styles/mainPage.css" />
    </head>
    <body>
    <%
    	if(request.getParameter("delete") != null && request.getParameter("imageId") != null)
    	{
    		Functions.deletePicture(request.getParameter("imageId"));
    		response.sendRedirect("/photo-album/pages/homePage.jsp");
    	}
    %>
    
        <div id="wrapper">
            
            <jsp:include page="header.jsp" />
               
            <div id="holder">
                <div id="bullshet">
                	
                    <div id="main">
                        <h1>
                        <%
                        if(session.getAttribute("user") != null && request.getParameter("userId") == null){
                        	out.print(((User)session.getAttribute("user")).getLName());
                        }
                        
                        if(request.getParameter("userId") != null){
                        	out.print(Functions.getUserById(request.getParameter("userId")).getLName());
                        }
                        
                        if(session.getAttribute("user") == null && request.getParameter("userId") == null){
                        	response.sendRedirect("/photo-album/pages/mainPage.jsp");
                        }
                        %>'s pics</h1>
                        
                        <table class="thumbnails">
                        
                        	<c:set var="counter" value="0"> </c:set>
                        	
                        	<%
	                        	List<Picture> pics = null;
	                        	if(request.getParameter("catId") != null)
	                    		{
	                        		pics = Functions.getPicturesForCategoryByCatId(request.getParameter("catId"));
	                    		}
	                        	else
	                        	{
	                        		if(request.getParameter("userId") != null)
	                        		{
	                        			pics = Functions.getAllPicturesByDateForUser(request.getParameter("userId"));
	                        		}
	                        		else
	                        		{
	                        			User u = (User)session.getAttribute("user");
		                        		pics = Functions.getAllPicturesByDateForUser(String.valueOf(u.getUserId()));
	                        		}
	                        	}
                        	
                        		request.setAttribute("pics", pics);
                        	%>
                        	
                        	<c:if test="${not empty criteria}">
	                         <c:set var="pics" value="${listOfFoundPictures}" />
	                        </c:if>
                        
	                        <c:forEach var="pic" begin="0" end="15" step="1" items='${pics}'> 
	                       
	                       		<c:if test="${counter==0}">
		                       			<tr>
		                       	</c:if>
	                       
                       			<c:if test="${counter%4==0 && counter!=0}">
                       					</tr>
		                       			<tr>
		                       	</c:if>
	                        	
		                        	<td>
		                        		
		                        		<%if(session.getAttribute("user") != null)
		                        		{
		                        			%>
		                        			<div class="img_ctrls">
		                                        <ul>	                                        	
		                                            <li><a href="/photo-album/pages/editPicturePage.jsp?imagePath=/photo-album${pic.picturePath}&imageId=${pic.pictureId}" title="Edit image"><img src="/photo-album/icons/edit.png" alt="Edit image" /></a></li>
		                                            <li><a href="/photo-album/pages/homePage.jsp?delete=true&imageId=${pic.pictureId}" title="Delete image"><img src="/photo-album/icons/delete.png" alt="Delete image" /></a></li>	                               
		                                        </ul>
	                                    	</div>
	                                    	<%
		                        		} %>
			                        	
		                        		
                                    	<div class="img_holder">
											<a href="#">
	                                            <img src="/photo-album${pic.picturePath}" alt="" />
	                                            <h4><c:out value='${pic.pictureName}'/></h4>
	                                            
	                                        </a>
                                        </div>
									</td>
								
								<c:set var="counter" value="${counter + 1}" />
							</c:forEach>
                        </table>
                        
                    </div>
                    <div id="sidebar">
                        <div id="searchbar">
                            <form action="/photo-album/SearchServlet" method="post">
                            	<input type="hidden" name="backTo" value="home">
                                <label for="search" accesskey="s">Search</label>
                                <br />
                                <input type="text" id="search" name="criteria" value="" class="stxt" />
                                <input type="submit" name="submit" value="Search" />
                            </form>
                        </div>
                        
                        <div id="categorieslist">
		                            <h3>Categories: </h3>
								
								<ul>
                            	<%!
                            		   public String showAllCategoriesForUser()
		                            	{
                            				Category rootC = Functions.getRootCategoryForUser("1");
                            			
                            				StringBuilder all = new StringBuilder();
                            				all.append("<li><a href=\"/photo-album/pages/homePage.jsp?catId=" + rootC.getCategoryId() + "\">" + rootC.getCategoryName());
                            				all.append("</a></li>");
                            				
                            				if(!(Functions.getCategoriesForCategory(Functions.getRootCategoryForUser("1")).isEmpty()))
                            				{
                            					showCategoriesForCategory(rootC, all);
                            				}
                            				
                            				
                            						
                            				return all.toString();
		                            	}
                            			public String showCategoriesForCategory(Category cat, StringBuilder all)
                            			{
                            				all.append("<ul>");
                            				
                           					for(Category c : Functions.getCategoriesForCategory(cat))
                           					{
                           						all.append("<li><a href=\"/photo-album/pages/homePage.jsp?catId=" + c.getCategoryId() + "\">");
                           						all.append(c.getCategoryName());
                           						all.append("</a></li>");
                           						if(!Functions.getCategoriesForCategory(c).isEmpty())
                           						{
                           							showCategoriesForCategory(c, all);
                           						}
                           					}
                            				
                            				all.append( "</ul>");
                            				return all.toString();
                            			}
                            	%>
                            	<%
                            		out.print(showAllCategoriesForUser());
                            	%>
                            	</ul>
                            	
								
                        </div>
                    </div>
                </div>
            </div>
            
            <jsp:include page="footer.jsp" />
           
        </div>
    </body>
</html>
